<?php
namespace backend\controllers\api;

use thirdparty\wechat\Application;
use EasyWeChat\Factory;
use Yii;
use yii\web\Controller;


class WechatController extends Controller
{

    public function beforeAction($action) {

        $currentAction = $action->id;

        $novalidActions = ['index'];

        if(in_array($currentAction,$novalidActions)) {

            $action->controller->enableCsrfValidation = false;
        }
        parent::beforeAction($action);

        return true;
    }

    public function actionIndex()
    {
        $appId = Yii::$app->request->get('appId','');

        $app = Yii::$app->wechat($appId);
        $server = $app->server;
        $user = $app->user;
        $server->push(function($message) use ($appId) {
            return (new Application(['message' => $message,'appId' => $appId]))->run();
        });

        $server->serve()->send();
    }

    public function actionSendMessage()
    {
        //text
        $message = array (
            'content' => "消息测试",
            'to' => ['openid1','openid2'],//int tagId //null
            'MsgType' => 'text'
        );

        // 图片
        $message = array (
            'mediaid' => "xxx",
            'to' => ['openid1','openid2'],//int tagId
            'MsgType' => 'image'
        );

        //语音消息
        $message = array (
            'mediaid' => "xxx",
            'to' => ['openid1','openid2'],//int tagId
            'MsgType' => 'voice'
        );

        //音乐消息
        $message = array (
            'mediaid' => "xxx",
            'to' => ['openid1','openid2'],//int tagId
            'MsgType' => 'music'
        );

        //视频消息
        $message = array (
            'mediaid' => "xxx",
            'to' => ['openid1','openid2'],//int tagId
            'MsgType' => 'video'
        );

        //卡券消息
        $message = array (
            'mediaid' => "xxx",
            'to' => ['openid1','openid2'],//int tagId
            'MsgType' => 'card'
        );

        $appId = "";

        (new Application(['message' => $message,'appId' => $appId]))->response();
    }


}
